package cn.edu.lingnan.controller;

import cn.edu.lingnan.entity.Banji;
import cn.edu.lingnan.service.BanjiService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: chen_man_cheng
 * @Description: //TODO
 * @Date: Create in 18:39 2020/6/7
 */
@RestController
@RequestMapping("banji")
public class BanjiController {
    @Autowired
    private BanjiService banjiService;

    @PostMapping("insert")
    public Object insert(Banji banji){
        return banjiService.insert(banji);
    }

    @GetMapping("queryAll")
    public Object queryAll(){
        return banjiService.queryAll();
    }

    @GetMapping("queryById/{id}")
//    引入服务熔断机制(回调本地方法)
    @HystrixCommand(fallbackMethod = "getDefaultBanji")
    public Object queryById(@PathVariable Integer id){
        Banji banji = banjiService.queryById(id);
        if (banji == null){
            throw new RuntimeException("出错了，没有改id的相关信息");
        }
        return banji;
    }

//  当服务熔断时，就调用这个本地服务，也就是说服务降级
    private Object getDefaultBanji(@PathVariable Integer id){
        return new Banji().setId(id).setName("班级不存在").setDb("no db");
    }

}
